package per.zzzfwd;

import per.zzzfwd.query.*;

import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        Specification spec1 = (fb, vb, qb) -> {
            Predicate[] predicates = Stream.of(
                qb.equal("province", "HuNan", "GuangDong"),
                qb.equal("weather", "windy"),
                qb.between("number", 1, 10)
            ).toArray(Predicate[]::new);
            return qb.and(predicates);
        };
        render(spec1);


        Specification spec2 = (fb, vb, qb) ->
            qb.and(
                qb.or(
                    qb.equal("province", "HuNan"),
                    qb.equal("province", "GuangDong")
                ),
                qb.or(
                    qb.equal("weather", "sunny"),
                    qb.equal("weather", "windy")
                )
            );
        render(spec2);


        Specification spec3 = (fb, vb, qb) ->
            qb.and(
                qb.or(
                    qb.equal("address", "HuNan"),
                    qb.equal("address", vb.and(vb.literal("GuangDong"), vb.list("GuangZhou", "ShenZhen")))
                ),
                qb.or(
                    qb.equal("weather", "sunny"),
                    qb.equal("weather", "windy")
                )
            );
        render(spec3);


        Specification spec4 = (fb, vb, qb) -> {
            Predicate[] predicates = Stream.of(
                qb.greaterThan("a", 1),
                qb.greaterThanOrEqualTo("b", 2),
                qb.lessThan("c", 10),
                qb.lessThanOrEqualTo("d", 100)
            ).toArray(Predicate[]::new);
            return qb.and(predicates);
        };
        render(spec4);
    }

    public static void render(Specification specification) {
        System.out.println(
            ((Renderable) specification.toPredicate(new FieldBuilder(), new ValueBuilder(), new QueryBuilder()))
                .render()
        );
    }
}
